package br.org.bertol.mestrado.engine;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public enum Objective {
    DEPEDENCIA {

        private List<List<Integer>> matrix = new ArrayList<List<Integer>>();

        @Override
        public List<List<Integer>> getWeekConstraint() {
            return matrix;
        }

        @Override
        public void loadWeekConstraint(final String source, final String system)
                throws IOException {

            matrix = readInput(source, system, "_depe.txt");
        }

        @Override
        public int getWeekConstraintHeight(final int classe,
                final int constraint) {
            return 1;
        }
        },
    METODO {

        private List<List<Integer>> matrix = new ArrayList<List<Integer>>();

        @Override
        public List<List<Integer>> getWeekConstraint() {
            return matrix;
        }

        @Override
        public void loadWeekConstraint(final String source, final String system)
                throws IOException {

            matrix = readInput(source, system, "_meth.txt");
        }

        @Override
        public int getWeekConstraintHeight(final int classe,
                final int constraint) {
            return matrix.get(classe - 1).get(constraint);
        }
        },
    ATRIBUTO {

        private List<List<Integer>> matrix = new ArrayList<List<Integer>>();

        @Override
        public List<List<Integer>> getWeekConstraint() {
            return matrix;
        }

        @Override
        public void loadWeekConstraint(final String source, final String system)
                throws IOException {

            matrix = readInput(source, system, "_attr.txt");
        }

        @Override
        public int getWeekConstraintHeight(final int classe,
                final int constraint) {
            return matrix.get(classe - 1).get(constraint);
        }
        };

    public abstract List<List<Integer>> getWeekConstraint();

    public abstract int getWeekConstraintHeight(final int classe, int constraint);

    public abstract void loadWeekConstraint(final String source,
            final String system) throws IOException;

    /**
     * @param objectivesNames
     * @return Array com os objetivos passados como par�mtros.
     */
    public static Objective[] valueOf(final String[] objectivesNames) {
        final Objective[] objectives = new Objective[objectivesNames.length];

        for (int i = 0; i < objectivesNames.length; i++) {
            objectives[i] = Objective.valueOf(objectivesNames[i]);
        }

        return objectives;
    }

    public static List<List<Integer>> loadStrongConstraint(final String source,
            final String system) throws IOException {
        return readInput(source, system, "_prece.txt");
    }

    protected static List<List<Integer>> readInput(final String source,
            final String system, final String typeInput) throws IOException {
        final List<List<Integer>> colluns = new ArrayList<List<Integer>>();

        final File inputFile = new File(source + "/" + system + typeInput);

        if (!inputFile.exists()) {
            return colluns;
        }

        final Scanner reader = new Scanner(inputFile);

        // caminha pela matriz de entrada
        while (reader.hasNext()) {
            String[] input = reader.nextLine().split(" ");

            List<Integer> row = new ArrayList<Integer>();

            // adiciona as classes a uma lista
            for (String c : input)
                row.add(Integer.parseInt(c));

            colluns.add(row);
        }

        return colluns;
    }
}
